package com.artpri.dao.impl;

import java.util.List;

import com.artpri.dao.AdPictureDao;
import com.artpri.model.AdPicture;
import com.artpri.service.AdPictureService.ImageState;

/**
 * 
 * @author ZhangSi
 *
 */
public class AdPictureDaoImpl extends HibernateDaoImpl<AdPicture> implements AdPictureDao{
	
	@Override
	public List<AdPicture> findAdPicturesByPlate(String plateName) {
		return findAdpicture(plateName, ImageState.ALL, "plate");
	}
	
	@Override
	public List<AdPicture> findAdPicturesByPosition(String positionName) {
		return findAdpicture(positionName, ImageState.ALL, "position");
	}
	
	@Override
	public List<AdPicture> findAdPicturesByPlate(String plateName, ImageState apState) {
		return findAdpicture(plateName, apState, "plate");
	}

	@Override
	public List<AdPicture> findAdPicturesByPosition(String positionName, ImageState state) {
		return findAdpicture(positionName, state, "position");
	}
	
	private List<AdPicture> findAdpicture(String name, ImageState state, String type) {
		if (type.equals("position")) {
			switch (state) {
			case ALL:
				return find("from AdPicture a where a.apPositionName=?0", name);
			case SHOW:
				return find("from AdPicture a where a.apPositionName=?0 and a.apState=?1", name, 1);
			case UNSHOW:
				return find("from AdPicture a where a.apPositionName=?0 and a.apState=?1", name, 0);
			default:
				break;
			}
		} else if(type.equals("plate")) {
			switch (state) {
			case ALL:
				return find("from AdPicture a where a.apPositionName like ?0", name+"%");
			case SHOW:
				return find("from AdPicture a where a.apPositionName like ?0 and a.apState=?1", name+"%", 1);
			case UNSHOW:
				return find("from AdPicture a where a.apPositionName like ?0 and a.apState=?1", name+"%", 0);
			default:
				break;
			}
		}
		return null;
	}
}
